﻿using System;
using System.Data;
using System.Drawing;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using HiauPdm.Bll;
using HiauPdm.Web;

namespace HiauPdm.Web
{
    public partial class CpSjWdBj : PageBase
    {
        protected override bool IsAccess()
        {
            return IsAccess("CpSjBj.aspx");
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                TextId.Value = Request.QueryString["CpSjId"];
                BtnSave.OnClientClick = "this.disabled=true;" + Page.ClientScript.GetPostBackEventReference(BtnSave, null) + ";return false;";
                BindData();
                TextMc.Focus();
            }
        }

        private void BindData()
        {
            if ((TextId.Value.Trim() != string.Empty) && BGlobal.IsNumeric(TextId.Value.Trim()))
            {
                DataSet ds = BCpSjBj.GetCpSjBjBySjId(Convert.ToInt32(TextId.Value.Trim()));
                if ((ds != null) && (ds.Tables[0].Rows.Count == 1))
                {
                    PanelEdit.Visible = true;
                    BtnSave.Enabled = true;
                    TextDh.Text = ds.Tables[0].Rows[0]["代号"].ToString();
                    TextLb.Text = ds.Tables[0].Rows[0]["类别"].ToString();
                    TextMc.Text = ds.Tables[0].Rows[0]["名称"].ToString();
                    TextGgxh.Text = ds.Tables[0].Rows[0]["规格型号"].ToString();
                    TextDw.Text = ds.Tables[0].Rows[0]["单位"].ToString();
                    TextBz.Text = ds.Tables[0].Rows[0]["备注"].ToString();
                    TextDjr.Text = ds.Tables[0].Rows[0]["登记人"].ToString();
                    TextBjr.Text = ds.Tables[0].Rows[0]["编辑人"].ToString();
                    TextBjsj.Text = ds.Tables[0].Rows[0]["编辑时间"].ToString();
                    if (TextDjr.Text != (string)Session["RealName"] && !IsAccess("编辑他人的产品设计"))
                    {
                        TextMc.ReadOnly = true;
                        TextGgxh.ReadOnly = true;
                        TextDw.ReadOnly = true;
                        TextBz.ReadOnly = true;
                        TextMc.BackColor = Color.FromArgb(0xE7F3FF);
                        TextGgxh.BackColor = Color.FromArgb(0xE7F3FF);
                        TextDw.BackColor = Color.FromArgb(0xE7F3FF);
                        TextBz.BackColor = Color.FromArgb(0xE7F3FF);
                    }
                    if (((int)ds.Tables[0].Rows[0]["大类"]) == 0)
                    {
                        Button2.Visible = Button3.Visible = false;
                    }
                    else if (((int)ds.Tables[0].Rows[0]["大类"]) == 1)
                    {
                        Button3.Visible = false;
                    }
                    BindSjwdData(Convert.ToInt32(TextId.Value.Trim()));
                }
            }
        }

        private void BindSjwdData(int iCpSjId)
        {
            DataSet ds = BCpSjBj.GetCpSjWdBySjId(iCpSjId);
            if (ds != null)
            {
                ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            }
            GridCpSjWd.DataSource = ds;
            GridCpSjWd.DataBind();
            SetCzQx();
        }

        private void SetCzQx()
        {
            if (TextDjr.Text != (string)Session["RealName"] && !IsAccess("编辑他人的产品设计"))
            {
                for (int i = 0; i < (GridCpSjWd.Rows.Count - 1); i++)
                {
                    if ((string)GridCpSjWd.DataKeys[i].Values[2] != (string)Session["RealName"])
                    {
                        ((TextBox)GridCpSjWd.Rows[i].Cells[1].Controls[1]).ReadOnly = true;
                        ((TextBox)GridCpSjWd.Rows[i].Cells[2].Controls[1]).ReadOnly = true;
                        ((FileUpload)GridCpSjWd.Rows[i].Cells[1].Controls[3]).Visible = false;
                        GridCpSjWd.Rows[i].Cells[5].Controls[1].Visible = false;
                        GridCpSjWd.Rows[i].Cells[5].Controls[3].Visible = false;
                    }
                }
            }
        }

        protected void BtnSave_Click(object sender, EventArgs e)
        {
            if (ShowErr(BCpSjBj.SaveCpSjWd(Convert.ToInt32(TextId.Value.Trim()), TextMc.Text, TextGgxh.Text, TextDw.Text, TextBz.Text, (string)Session["RealName"], GridCpSjWd)))
            {
                BindSjwdData(Convert.ToInt32(TextId.Value.Trim()));
            }
            else
            {
                Response.Redirect(Request.Url.PathAndQuery);
            }
        }

        protected void GridCpSjWd_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if ((e.Row.RowType == DataControlRowType.DataRow) && (GridCpSjWd.DataKeys[e.Row.DataItemIndex].Value.ToString() == string.Empty))
            {
                ((Literal)e.Row.Cells[0].Controls[1]).Text = "添加";
                e.Row.Cells[5].Controls[1].Visible = false;
                e.Row.Cells[5].Controls[3].Visible = false;
                if (Convert.ToInt32(TextDh.Text.Substring(4, 1)) >= 1 && Convert.ToInt32(TextDh.Text.Substring(4, 1)) <= 6)
                {
                    if (TextDjr.Text == (string)Session["RealName"] || IsAccess("编辑他人的产品设计"))
                    {
                        e.Row.Cells[5].Controls[5].Visible = true; // 导入明细
                    }
                }
            }
        }

        protected void GridCpSjWd_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "DownloadDoc")
            {
                Response.Redirect("DownloadFile.aspx?id=" + BGlobal.Encrypt(string.Format("{0}|{1}_{2}.{3}", GridCpSjWd.DataKeys[Convert.ToInt32(e.CommandArgument)].Values[1], TextDh.Text.Trim(), ((TextBox)GridCpSjWd.Rows[Convert.ToInt32(e.CommandArgument)].Cells[1].Controls[1]).Text.Trim(), BGlobal.HexToString(GridCpSjWd.DataKeys[Convert.ToInt32(e.CommandArgument)].Values[1].ToString().Split('_')[1])), "Download"));
            }
            else if (e.CommandName == "ImportData")
            {
                MsgBox(BCpSjBj.ImportCpSjMx(Convert.ToInt32(TextId.Value.Trim()), (FileUpload)GridCpSjWd.Rows[GridCpSjWd.Rows.Count - 1].Cells[1].Controls[3], (string)Session["RealName"]), true);
            }
        }

        protected void GridCpSjWd_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            if (!ShowErr(BCpSjBj.DelCpSjWd((int)GridCpSjWd.DataKeys[e.RowIndex].Value)))
            {
                Response.Redirect(Request.Url.PathAndQuery);
            }
        }
    }
}